﻿#include "LensManager.h"
#include <QDebug>
namespace Qly {


void LensManager::setLensCount(int n)
{
    m_lensCount = qBound(1, n, MAX_LENS);
    if(m_curLens >= m_lensCount)
    {
        m_curLens = m_lensCount - 1;
    }
}

void LensManager::setRulerPosition(int x_pix, int y_pix)
{
    m_rulerX_pix = x_pix;
    m_rulerY_pix = y_pix;
}

QPoint LensManager::rulerPosition()
{
    return  QPoint(m_rulerX_pix, m_rulerY_pix);
}

LensData &LensManager::currentLens()
{
    return m_lens[m_curLens];
}

int LensManager::currentLensIndex() const
{
    return m_curLens;
}

void LensManager::setLensByIndex(int index, LensData d)
{
    if(index < 0 || index > m_lensCount -1)
    {
        return;
    }
    m_lens[index] = d;
}

void LensManager::loadSettings(QSettings &settings)
{
    m_lensCount = settings.value("Lens/Count", 5).toInt();
    m_curLens = settings.value("Lens/Index", 0).toInt();
    m_rulerX_pix = settings.value("Lens/RulerX", 200).toInt();
    m_rulerY_pix = settings.value("Lens/RulerY", 200).toInt();
    m_rulerLineWidth = settings.value("Lens/RulerLineWidth", 2).toInt();
    m_rulerColor = (Qt::GlobalColor)settings.value("Lens/RulerColor", (int)Qt::black).toInt();
    m_rulerFontSize = settings.value("Lens/RulerFontSize", 28).toInt();

    for(int i = 0; i < m_lensCount; i++)
    {
        LensData d;
        QString prefix = QString("Lens_%1").arg(i);
        d.lens = (enum LENS) settings.value(prefix + "/Lens", LENS_NULL).toInt();
        d.displayName = settings.value(prefix + "/displayName", "5X").toString();
        d.comment = settings.value(prefix + "/comment", "").toString();
        d.rulerText = settings.value(prefix + "/rulerText", d.displayName).toString();
        d.pixelSize_um = settings.value(prefix + "/pixelSize_um", 1).toDouble();
        d.numericalAperture = settings.value(prefix + "/numericalAperture", 0.9).toDouble();
        d.rulerLength_um = settings.value(prefix + "/rulerLength_um", 200).toDouble();
        d.xCompensate_um = settings.value(prefix + "/xCompensate_um", 0).toDouble();
        d.yCompensate_um = settings.value(prefix + "/yCompensate_um", 0).toDouble();
        d.workDistance_um = settings.value(prefix + "/WorkingDistance_um", 10).toDouble();

        setLensByIndex(i, d);
    }
    qDebug() << "Lens Count = " << m_lensCount;
    emit lensChanged(-1, m_curLens);
}

void LensManager::writeSettings(QSettings &settings) const
{
    settings.setValue("Lens/Count", m_lensCount);
    settings.setValue("Lens/Index", m_curLens);
    settings.setValue("Lens/RulerX", m_rulerX_pix);
    settings.setValue("Lens/RulerY", m_rulerY_pix);
    settings.setValue("Lens/RulerLineWidth", m_rulerLineWidth);
    settings.setValue("Lens/RulerColor", (int)m_rulerColor);
    settings.setValue("Lens/RulerFontSize", m_rulerFontSize);
    for(int i = 0; i < m_lensCount; i++)
    {
        QString prefix = QString("Lens_%1").arg(i);
        settings.setValue(prefix + "/Lens", m_lens[i].lens);
        settings.setValue(prefix + "/displayName", m_lens[i].displayName);
        settings.setValue(prefix + "/comment", m_lens[i].comment);
        settings.setValue(prefix + "/rulerText", m_lens[i].rulerText);
        settings.setValue(prefix + "/pixelSize_um", m_lens[i].pixelSize_um);
        settings.setValue(prefix + "/numericalAperture", m_lens[i].numericalAperture);
        settings.setValue(prefix + "/rulerLength_um", m_lens[i].rulerLength_um);
        settings.setValue(prefix + "/xCompensate_um", m_lens[i].xCompensate_um);
        settings.setValue(prefix + "/yCompensate_um", m_lens[i].yCompensate_um);
        settings.setValue(prefix + "/WorkingDistance_um", m_lens[i].workDistance_um);
    }
}

void LensManager::setLens(int oldlensIndex, int newlensIndex)
{
    Q_UNUSED(oldlensIndex);

    qDebug() << "LensManager::setLens(" << oldlensIndex << ", " << newlensIndex << ")" << ", m_curLens = " << m_curLens;
    if(m_curLens == newlensIndex)
    {
        return;
    }
    if(newlensIndex >= m_lensCount || newlensIndex < 0)
    {
        return;
    }
    int old = m_curLens;
    m_curLens = newlensIndex;
    qDebug() << "LensManager emit lensChanged(" << old << ", " << newlensIndex << ")";
    emit lensChanged(old, newlensIndex);
}

LensData &LensManager::lensByIndex(int n)
{
    return m_lens[n];
}

double LensManager::pixelSize_um() const
{
    return m_lens[m_curLens].pixelSize_um;
}

QString LensManager::lensName() const
{
    return m_lens[m_curLens].displayName;
}

}

